package com.qyer.search.model.mutable;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.collections4.CollectionUtils;

import java.util.*;
/**
 * Created by tangzheng on 16/9/5.
 */
public class MarketingListInfo {

  private List<Integer> mList;
  private Set<Integer> mSet;
  private long start = 0;
  private long end = 0;
  private int type;
  private int placeId;

  public MarketingListInfo(int size) {
    mList = Lists.newArrayListWithCapacity(size);
    mSet = Sets.newHashSetWithExpectedSize(size);
  }

  public void addMI(String[] strs){
    int l = strs.length;
    for(int i = 0; i < l; i++){
      try{
        this.mList.add(Integer.parseInt(strs[i]));
        this.mSet.add(Integer.parseInt(strs[i]));
      }catch (Exception e){
        e.printStackTrace();
      }
    }
  }

  public boolean isValid(long t){
    if(t >= start && t < end)
      return true;
    return false;
  }

  public int getType() {
    return type;
  }

  public void setType(int type) {
    this.type = type;
  }

  public int getPlaceId() {
    return placeId;
  }

  public void setPlaceId(int placeId) {
    this.placeId = placeId;
  }

  public long getStart() {
    return start;
  }

  public void setStart(long start) {
    this.start = start;
  }

  public long getEnd() {
    return end;
  }

  public void setEnd(long end) {
    this.end = end;
  }

  public List<Integer> getmList() {
    return mList;
  }

  public void setmList(List<Integer> mList) {
    this.mList = mList;
  }

  public Set<Integer> getmSet() {
    return mSet;
  }

  public void setmSet(Set<Integer> mSet) {
    this.mSet = mSet;
  }

  @Override public String toString() {
    return "MarketingListInfo{" +
      "mList=" + mList +
      ", mSet=" + mSet +
      ", start=" + start +
      ", end=" + end +
      ", type=" + type +
      ", placeId=" + placeId +
      '}';
  }

  @Override public boolean equals(Object o) {
    if (this == o)
      return true;
    if (o == null || getClass() != o.getClass())
      return false;

    MarketingListInfo that = (MarketingListInfo) o;

    if (start != that.start)
      return false;
    if (end != that.end)
      return false;
    if (type != that.type)
      return false;
    if (placeId != that.placeId)
      return false;
    if (!mList.equals(that.mList))
      return false;
    return mSet.equals(that.mSet);

  }

  @Override public int hashCode() {
    int result = mList.hashCode();
    result = 31 * result + mSet.hashCode();
    result = 31 * result + (int) (start ^ (start >>> 32));
    result = 31 * result + (int) (end ^ (end >>> 32));
    result = 31 * result + type;
    result = 31 * result + placeId;
    return result;
  }

  public void addMarketInfo(MarketingListInfo marketingListInfo){
    if(marketingListInfo == null)
      return;
    List<Integer> list = marketingListInfo.getmList();
    if(CollectionUtils.isEmpty(mList)){
      if(CollectionUtils.isNotEmpty(list)){
        this.mList.addAll(list);
        this.mSet.addAll(list);
      }
    }else {
      for (int i : list) {
        if (!mSet.contains(i)) {
          this.mList.add(i);
          this.mSet.add(i);
        }
      }
    }
  }

  public boolean isEmpty(){
    if(CollectionUtils.isEmpty(mList))
      return true;
    return false;
  }
}
